package com.group3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.*;
import com.group3.domain.*;
import com.group3.pojo.receive.TLessonListReceive;
import com.group3.pojo.receive.TStudentListReceive;
import com.group3.pojo.result.LessonResult;
import com.group3.service.TeacherLessonService;
import com.group3.utils.NotNull;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * TeacherLessonServiceImpl
 *
 * @author MHH
 * 2021/06/10 16:51
 **/
@Service
public class TeacherLessonServiceImpl implements TeacherLessonService {

    @Resource
    private LessonMapper lessonMapper;
    @Resource
    private LessonTeacherMapper lessonTeacherMapper;
    @Resource
    private StudentInfoByLessonMapper studentInfoByLessonMapper;

    @Override
    //查询项目课程表
    public Result<List<LessonResult>> selectLessonList(TLessonListReceive lessonListReceive) {

        LessonExample example = new LessonExample();
        LessonExample.Criteria criteria = example.createCriteria();

//          限制查找范围是操作老师所上的课
//          区别实训老师与责任老师:不是责任老师就不传teacherId
        if (NotNull.judge(lessonListReceive.getTeacherZ())) {
            criteria.andTeacherIdEqualTo(lessonListReceive.getTeacherZ());
        }
//          查询开始时间小于课程开始时间
        if (NotNull.judge(lessonListReceive.getStartDateTime())) {
            criteria.andStartTimeLessThanOrEqualTo(lessonListReceive.getStartDateTime());
        }
//          查询结束时间大于课程结束时间
        if (NotNull.judge(lessonListReceive.getEndDateTime())) {
            criteria.andEndTimeGreaterThanOrEqualTo(lessonListReceive.getEndDateTime());
        }
//          课程名 - 模糊查询似
        if (NotNull.judge(lessonListReceive.getLessonName())) {
            criteria.andLessonNameLike("%" + lessonListReceive.getLessonName() + "%");
        }
//          添加责任老师
        if (NotNull.judge(lessonListReceive.getTeacherZ())) {
            criteria.andTeacherIdEqualTo(lessonListReceive.getTeacherZ());
        }

        // 根据实训老师查询课程（lesson_teacher表）
        LessonTeacherExample lessonTeacherExample = new LessonTeacherExample();
        lessonTeacherExample.setDistinct(true);
        // 得到一个课程集合
        List<LessonTeacherKey> lessonTeacherKeys;
        lessonTeacherKeys = lessonTeacherMapper.selectByExample(lessonTeacherExample);
        // 取出课程名集合
        List<String> lessonName = new ArrayList<>();
        for (LessonTeacherKey lessonTeacherKey : lessonTeacherKeys) {
            lessonName.add(lessonTeacherKey.getLessonName());
        }


//        添加集合条件
        criteria.andLessonNameIn(lessonName);

//        1.先开启分页
        PageHelper.startPage(lessonListReceive.getPageNumber(), lessonListReceive.getPageSize());

//        2.执行SQL语句
        List<LessonResult> lessonResults = lessonMapper.selectLessonInfo(example);

//        3.获取分页信息
        PageInfo<LessonResult> lessonResultPageInfo = new PageInfo<>(lessonResults);

        return ResultUtil.success("查询成功", lessonResults, lessonResultPageInfo.getTotal());
    }

    @Override
    //根据课程id查询学生
    public Result selectStudentByLesson(TStudentListReceive tStudentListReceive) {
        //1.new一个对应的example
        StudentInfoByLessonExample example = new StudentInfoByLessonExample();
        StudentInfoByLessonExample.Criteria criteria = example.createCriteria();

        //2.为example添加查询条件
        //课程ID
        if (NotNull.judge(tStudentListReceive.getLessonId()))
            criteria.andLessonIdEqualTo(tStudentListReceive.getLessonId());
        //小组名称
        if (NotNull.judge(tStudentListReceive.getGroupName()))
            criteria.andGroupNameEqualTo(tStudentListReceive.getGroupName());
        //专业
        if (NotNull.judge(tStudentListReceive.getProfessionName()))
            criteria.andProfessionNameEqualTo(tStudentListReceive.getProfessionName());
        //年级
        if (NotNull.judge(tStudentListReceive.getGrade()))
            criteria.andGradeEqualTo(tStudentListReceive.getGrade());
        //班级
        if (NotNull.judge(tStudentListReceive.getClassId()))
            criteria.andClassIdEqualTo(tStudentListReceive.getClassId());
        //姓名
        if (NotNull.judge(tStudentListReceive.getStudentName()))
            criteria.andStudentNameLike("%" + tStudentListReceive.getStudentName() + "%");
        //学号
        if (NotNull.judge(tStudentListReceive.getStudentId()))
            criteria.andClassIdEqualTo(tStudentListReceive.getStudentId());


        //        1.先开启分页
        PageHelper.startPage(tStudentListReceive.getPageNumber(), tStudentListReceive.getPageSize());

        //        2.执行SQL语句
        List<StudentInfoByLesson> studentInfoByLesson = studentInfoByLessonMapper.selectByExample(example);

        //        3.获取分页信息
        PageInfo<StudentInfoByLesson> page = new PageInfo<>(studentInfoByLesson);

        return ResultUtil.success("查询成功", studentInfoByLesson, page.getTotal());
    }
}
